package bg.cinema_system.beans;

import java.sql.SQLException;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

import bg.cinema_system.pojo.UserTickets;
import bg.cinema_system.service.UserTicketsService;

@ManagedBean(name = "staffTicketsList")
@RequestScoped
public class StaffTicketsListBean {

	private int user_id;
	private String username;
	private boolean validReservation;
	private boolean validUsername;
	private List<UserTickets> staffTickets;

	public int getUser_id() {
		return user_id;
	}

	public void setUser_id(int user_id) {
		this.user_id = user_id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) throws SQLException {
		this.username = username;

		setUsername();
		getStaffTicketsList();

		this.username.replaceAll("<", "&gt;").replaceAll(">", "&lt;");
	}

	public List<UserTickets> getStaffTickets() {
		return staffTickets;
	}

	public void setStaffTickets(List<UserTickets> staffTickets) {
		this.staffTickets = staffTickets;
	}

	// Set the "username" and "user_id"
	public void setUsername() throws SQLException {
		this.user_id = UserTicketsService.getUserId(this.username);

		if (this.user_id == 0) {
			this.setValidUsername(false);
		} else {
			this.setValidUsername(true);
		}
	}

	// Get user's reserved tickets
	public void getStaffTicketsList() throws SQLException {
		this.staffTickets = UserTicketsService.getUserTicketsList(this.user_id);

		if (this.validUsername) {
			if (this.staffTickets.isEmpty()) {
				this.setValidReservation(false);
			} else {
				this.setValidReservation(true);
			}
		} else {
			this.setValidReservation(true);
		}
	}

	public boolean isValidReservation() {
		return validReservation;
	}

	public void setValidReservation(boolean validReservation) {
		this.validReservation = validReservation;
	}

	public boolean isValidUsername() {
		return validUsername;
	}

	public void setValidUsername(boolean validUsername) {
		this.validUsername = validUsername;
	}

	@Override
	public String toString() {
		return "StaffTicketsListBean [user_id=" + user_id + ", username="
				+ username + ", validReservation=" + validReservation
				+ ", validUsername=" + validUsername + ", staffTickets="
				+ staffTickets + "]";
	}

}
